package com.hqyj.project.service.impl;

import com.hqyj.project.dto.SettingDto;
import com.hqyj.project.dto.UserDto;
import com.hqyj.project.entity.Attendance;
import com.hqyj.project.entity.AttendanceStatistics;
import com.hqyj.project.entity.Leave;
import com.hqyj.project.entity.Setting;
import com.hqyj.project.mapper.AttendanceMapper;
import com.hqyj.project.service.AttendanceService;
import com.hqyj.project.utils.ClockInUtil;
import com.hqyj.project.utils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public CommonResult clockIn(Integer userId) {
        Attendance attendance = new Attendance();
        String[] attendanceList = ClockInUtil.clockIn();
        attendance.setAttendanceId(userId);
        attendance.setAttendanceRecord(attendanceList[2]);
        attendance.setAttendanceDate(attendanceList[0]);

        //
        AttendanceStatistics attendanceStatistics = new AttendanceStatistics();
        attendanceStatistics.setClockUserId(userId);
        attendanceStatistics.setClockDate(attendanceList[0]);

        // 查询用户是否存在当天考勤的记录
        List<AttendanceStatistics> attendanceStatisticsList = attendanceMapper.selectAttendanceStatistics(attendanceStatistics);

        System.out.println(attendanceStatistics);
        if (attendanceStatisticsList.size() == 0) {
            if (attendanceList[7] == "") {
                attendanceStatistics.setMornSignIn("未打卡");
            } else {
                attendanceStatistics.setMornSignIn(attendanceList[7]);
            }
            if (attendanceList[8] == "") {
                attendanceStatistics.setMornSignOut("未打卡");
            } else {
                attendanceStatistics.setMornSignOut(attendanceList[8]);
            }
            if (attendanceList[9] == "") {
                attendanceStatistics.setAfterSignIn("未打卡");
            } else {
                attendanceStatistics.setAfterSignIn(attendanceList[9]);
            }
            if (attendanceList[10] == "") {
                attendanceStatistics.setAfterSignOut("未打卡");
            } else {
                attendanceStatistics.setAfterSignOut(attendanceList[10]);
            }
            attendanceMapper.insertAttendanceStatisticsById(attendanceStatistics);
        } else {
            attendanceStatistics.setMornSignIn(attendanceList[7]);
            attendanceStatistics.setMornSignOut(attendanceList[8]);
            attendanceStatistics.setAfterSignIn(attendanceList[9]);
            attendanceStatistics.setAfterSignOut(attendanceList[10]);
            attendanceMapper.updateAttendanceStatisticsById(attendanceStatistics);
        }

        // 传递用户id,当前时间段的打卡内容去数据库查，看用户是否已经打卡（防止重复打卡）
        List<Attendance> attendance1 = attendanceMapper.selectAttendance(attendance);
        // 存储用户当前迟到、早退、请假的次数
        // 如果员工的迟到、早退、请假的次数达到一个峰值，就对员工发出警告（10次）
        Integer late=0;
        Integer early=0;

        if (attendance1.size() == 0) {
            if (attendanceList[2].equals("当前不在上班打卡时间范围内")) {
                return new CommonResult(500, attendanceList[2], attendance);
            } else {
                attendance.setAttendanceId(userId);
                attendance.setAttendanceRecord(attendanceList[2]);
                attendance.setAttendanceDate(attendanceList[0]);
                attendance.setAttendanceTime(attendanceList[1]);
                // 判断员工打卡时是否存在迟到或早退，存在就添加一个备注
                if (!attendanceList[3].equals("正常打卡")) {
                    attendance.setAttendanceRemark(attendanceList[3]);
                    // 员工打卡时是否存在迟到或早退,就对迟到或者早退的次数加1

                    Setting setting = new Setting();
                    setting.setUserId(userId);
                    setting.setSettingDate(attendanceList[12]);
                    // 查询该员工本月是否存在早退或者迟到行为
                    List<Setting> settingList = attendanceMapper.selectSetting(setting);
                    // 本月还没有迟到和早退记录
                    if (settingList.size() == 0) {
                        // 判断员工时迟到还是早退
                        if (attendanceList[3].equals("迟到")) {
                            setting.setSettingLeave(0);
                            setting.setSettingEarly(0);
                            setting.setSettingLate(1);
                        } else {
                            setting.setSettingEarly(1);
                            setting.setSettingLeave(0);
                            setting.setSettingLate(0);
                        }
                        attendanceMapper.insertSettingByUserId(setting);
                    } else {
                        if (attendanceList[3].equals("迟到")) {
                            // 目前员工迟到的次数
                            late=settingList.get(0).getSettingLate();
                            // 加上本次迟到
                            setting.setSettingLate(late + 1);

                        } else {
                            // 早退
                            early=settingList.get(0).getSettingEarly();

                            setting.setSettingEarly(early+ 1);
                        }
                        attendanceMapper.updateSettingByUserId(setting);
                    }
                }
                attendanceMapper.insertAttendanceByUserId(attendance);
                return new CommonResult(200, attendanceList[2], attendance);
            }
        } else {
            return new CommonResult(500, "已经打卡，请勿重复打卡！");
        }

    }

    @Override
    public CommonResult queryAttendUser(Integer userId) {

        AttendanceStatistics attendanceStatistics = new AttendanceStatistics();
        attendanceStatistics.setClockUserId(userId);
        List<UserDto> userDtos = attendanceMapper.selectAttendanceStatisticsAndUser(attendanceStatistics);

        return new CommonResult(200, "查询成功", userDtos);


    }

    @Override
    public CommonResult queryLeave(Leave leave) {
        List<Leave> leaves = attendanceMapper.selectLeaveById(leave);
        if(leaves.get(0).getLeaveSubmit()==null){
            return new CommonResult(500, "没有请假信息");
        }
        return new CommonResult(200, "查询成功", leaves);
    }

    @Override
    public CommonResult querySettingEarly() {
        List<SettingDto> earlyDtos = attendanceMapper.selectSettingDtoEarly();
        return new CommonResult(200, "查询成功", earlyDtos);
    }

    @Override
    public CommonResult querySettingLate() {
        List<SettingDto> lateDtos = attendanceMapper.selectSettingDtoLate();
        return new CommonResult(200, "查询成功", lateDtos);
    }

    @Override
    public CommonResult querySettingLeave() {
        List<SettingDto> leaveDtos = attendanceMapper.selectSettingDtoLeave();
        return new CommonResult(200, "查询成功",leaveDtos);
    }

    @Override
    public CommonResult report() {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        String settingDate = now.format(dateTimeFormatter);
        SettingDto settingDto = new SettingDto();
        settingDto.setSettingDate(settingDate);
        List<SettingDto> settingDtoList = attendanceMapper.selectSettingDtoReport(settingDto);
        return new CommonResult(200, "查询成功",settingDtoList);
    }
}
